var old = null;
var limit = -1;
var first = true;
var montior = null;
jQuery(function($) {
	Highcharts.setOptions({
		global : {
			useUTC : false
		}
	}); 
	$('.widget-container-col').sortable({
        connectWith: '.widget-container-col',
		items:'> .widget-box',
		handle: ace.vars['touch'] ? '.widget-header' : false,
		cancel: '.fullscreen',
		opacity:0.8,
		revert:true,
		forceHelperSize:true,
		placeholder: 'widget-placeholder',
		forcePlaceholderSize:true,
		tolerance:'pointer',
		start: function(event, ui) {
			//when an element is moved, it's parent becomes empty with almost zero height.
			//we set a min-height for it to be large enough so that later we can easily drop elements back onto it
			ui.item.parent().css({'min-height':ui.item.height()})
			//ui.sender.css({'min-height':ui.item.height() , 'background-color' : '#F5F5F5'})
		},
		update: function(event, ui) {
			ui.item.parent({'min-height':''})
			//p.style.removeProperty('background-color');
		}
    });
	montior = new Montior({width:$("#machine"+" .heap").width()});
	montior.init();
	get();
});
function get(){
	$.get(window.global.getFinalUrl(window.global.ctx+"/monitor/system"),null, function(result) {
		if(result.status==200){
			var data = result.data;
			if(first){
				var d = new Date()
					d.setTime(data.jvm.startTime);
				$("#startTime").text(d.format("yyyy-MM-dd hh:mm:ss"));
				$("#jvmName").text(data.jvm.jvmName);
				$("#osName").text(data.os.osName+" "+data.os.osArch);
				var list = data.os.osDiskList;
				if(list){
					var disks = [];
					var useddata = [];
					var diskdata = [];
					var index = 2;
					for(var d in list){
						var diskItem = list[d];
						disks.push(diskItem.name);
						useddata.push(parseFloat(window.global.bytesToSizeFormKB(diskItem.total-diskItem.free,index)));
						diskdata.push(parseFloat(window.global.bytesToSizeFormKB(diskItem.total,index)));
					}
					montior.getDisk(disks,useddata,diskdata);
				}
			}else{
				var list = data.os.osDiskList;
				if(list){
					var useddata = [];
					var diskdata = [];
					var index = 2;
					for(var d in list){
						var diskItem = list[d];
						useddata.push(parseFloat(window.global.bytesToSizeFormKB(diskItem.total-diskItem.free,index)));
						diskdata.push(parseFloat(window.global.bytesToSizeFormKB(diskItem.total,index)));
					}
					montior.osDisk.series[0].setData(useddata);
					montior.osDisk.series[1].setData(diskdata);
				}
			}
			
			var now = data.now;
			var d = null;
			//堆
			var index = 2;
			d = parseFloat(window.global.bytesToSize(data.jvm.heapMemory.used,index));
			montior.jvmHeap.series[0].addPoint([ now, d ], false, true);
			d = parseFloat(window.global.bytesToSize(data.jvm.heapMemory.alloc,index));
			montior.jvmHeap.series[1].addPoint([ now, d ], false,true);
			d = parseFloat(window.global.bytesToSize(data.jvm.heapMemory.max,index));
			montior.jvmHeap.series[2].addPoint([ now, d ], true,true);
			//非堆
			d = parseFloat(window.global.bytesToSize(data.jvm.nonHeapMemory.used,index));
			montior.jvmNonHeap.series[0].addPoint([ now, d ], false, true);
			d = parseFloat(window.global.bytesToSize(data.jvm.nonHeapMemory.alloc,index));
			montior.jvmNonHeap.series[1].addPoint([ now, d ], false,true);
			d = parseFloat(window.global.bytesToSize(data.jvm.nonHeapMemory.max,index));
			montior.jvmNonHeap.series[2].addPoint([ now, d ], true,true);
			//cpu
			d = data.jvm.cpuRatio;
			montior.jvmCupInfo.series[0].addPoint([ now, parseFloat(parseFloat(d).toFixed(2)) ], true, true);
			//线程
			$("#machine .threadinfoStat").text("活动线程："+data.jvm.threadsInfo.threadsCurrent+"" +
					"，守护线程："+data.jvm.threadsInfo.threadsDaemon+"" +
					"，实时线程峰值："+data.jvm.threadsInfo.threadsPeak+"" +
					"，已启动线程总数："+data.jvm.threadsInfo.threadsTotal+"");
			d = parseInt(data.jvm.threadsInfo.threadsCurrent);
			montior.jvmThreadInfo.series[0].addPoint([ now, d ], false, true);
			d = parseInt(data.jvm.threadsInfo.threadsDaemon);
			montior.jvmThreadInfo.series[1].addPoint([ now, d ], true,true);
			
			index = 3;
			d = parseFloat(window.global.bytesToSize(data.os.osMemoryTotal-data.os.osMemoryFree,index));
			montior.osMem.series[0].addPoint([ now, d ], false,true);
			d = parseFloat(window.global.bytesToSize(data.os.osMemoryTotal,index));
			montior.osMem.series[1].addPoint([ now, d ], true,true);

			d = parseFloat(window.global.bytesToSize(data.os.osSwapTotal-data.os.osSwapFree,index));
			montior.osSwap.series[0].addPoint([ now, d ], false,true);
			d = parseFloat(window.global.bytesToSize(data.os.osSwapTotal,index));
			montior.osSwap.series[1].addPoint([ now, d ], true,true);
			
			//cpu
			d = data.os.osCpuRatio.replace("%","");
			d = parseFloat(parseFloat(d,10).toFixed(2));
			montior.osCpu.series[0].addPoint([ now,(d)], true, true);
			if(first){
				pageInit();
			}
			first = false;
			if(start){
				setTimeout(function(){
					get();
				},montior.internal);
			}
		}
	});
	pageInit();
}
